//
//  VistorView.m
//  weiboObjectC
//
//  Created by 隗杰 on 16/2/20.
//  Copyright © 2016年 kkk. All rights reserved.
//

#import "VistorView.h"
#import <Masonry.h>
#import "UILabel+extension.h"
#import "UIButton+extension.h"
@interface VistorView ()
@property (strong,nonatomic)UIImageView *largeImageView;
@property (strong,nonatomic)UIImageView *houseImageView;
@property (strong,nonatomic)UILabel *tipLabel;
@property (strong,nonatomic)UIButton *loginButn;
@property (strong,nonatomic)UIButton *registerButn;
@property (strong,nonatomic)UIImageView *backImageView;
@end

@implementation VistorView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupUI];
        
        CGFloat value = 237 / 255.0;
        self.backgroundColor = [[UIColor alloc] initWithRed:value green:value blue:value alpha:1];
    }
    return self;
}
// 设置 ui
- (void)setupUI {
    // 添加
    [self addSubview:self.largeImageView];
    [self addSubview:self.backImageView];
    [self addSubview:self.houseImageView];
    [self addSubview:self.tipLabel];
    [self addSubview:self.loginButn];
    [self addSubview:self.registerButn];
    
    // 自动布局 ui
    [self.largeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.centerY.equalTo(self.mas_centerY).offset = -60;
    }];
    [self.houseImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.largeImageView);
    }];
    [self.tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.top.equalTo(self.largeImageView.mas_bottom).offset = 16;
        make.width.equalTo(@230);
        make.height.equalTo(@60);
    }];
    [self.loginButn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.tipLabel.mas_left);
        make.top.equalTo(self.tipLabel.mas_bottom).offset = 16;
        make.size.mas_equalTo(CGSizeMake(100, 30));
    }];
    [self.registerButn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.tipLabel.mas_right);
        make.top.equalTo(self.tipLabel.mas_bottom).offset = 16;
        make.size.mas_equalTo(CGSizeMake(100, 30));
    }];
    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.largeImageView);
    }];
    [self.loginButn addTarget:self action:@selector(loginButnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.registerButn addTarget:self action:@selector(registerButnClick) forControlEvents:UIControlEventTouchUpInside];
}

- (void)loginButnClick {
    if (self.delegate) {
        
        if ([self.delegate respondsToSelector:@selector(loginClick:)]) {
            [self.delegate loginClick:self];
        }
        
    }
}

- (void)registerButnClick {
    if (self.delegate) {
        if ([self.delegate respondsToSelector:@selector(registerClick:)]) {
            [self.delegate registerClick:self];
        }
    }
}

- (void)setupInfoWith:(NSString *)imageName and:(NSString *)title {
    if (imageName == nil) {
        [self startAnimation];
    } else {
        self.largeImageView.image = [UIImage imageNamed:imageName];
        self.tipLabel.text = title;
        self.houseImageView.hidden = YES;
        self.backImageView.hidden = YES;
    }
    
}

- (void)startAnimation {
    CABasicAnimation *ani = [[CABasicAnimation alloc] init];
    ani.keyPath = @"transform.rotation";
    ani.repeatCount = MAXFLOAT;
    ani.duration = 20;
    ani.toValue = @(M_PI * 2);
    ani.removedOnCompletion = NO;
    [self.largeImageView.layer addAnimation:ani forKey:nil];

}

// 懒加载视图
- (UIImageView *)largeImageView {
    if (!_largeImageView) {
        _largeImageView = [[UIImageView alloc] init];
        _largeImageView.image = [UIImage imageNamed:@"visitordiscover_feed_image_smallicon"];
    }
    return _largeImageView;
}

- (UIImageView *)houseImageView {
    if (!_houseImageView) {
        _houseImageView = [[UIImageView alloc] init];
        _houseImageView.image = [UIImage imageNamed:@"visitordiscover_feed_image_house"];
    }
    return  _houseImageView;
}

- (UILabel *)tipLabel {
    if (!_tipLabel) {
        UIColor *color = [UIColor lightGrayColor];
        _tipLabel = [UILabel labelWithTitle:@"关注一些人,回这里看看有什么惊喜" andFontSize:15 andFontColor:color];
        _tipLabel.textAlignment = NSTextAlignmentCenter;
//        _tipLabel.frame.size.width = 130;
        _tipLabel.numberOfLines = 0;
        
    }
    return _tipLabel;
}

- (UIButton *)loginButn {
    if (!_loginButn) {
        UIColor *color = [UIColor darkGrayColor];
        _loginButn = [UIButton butnWithImageName:nil andTitle:@"登陆" andBackgrandImage:@"common_button_white_disable" andFontSize:14 andFontColor:color];
    }
    return _loginButn;
}

- (UIImageView *)backImageView {
    if (!_backImageView) {
        _backImageView = [[UIImageView alloc] init];
        _backImageView.image = [UIImage imageNamed:@"visitordiscover_feed_mask_smallicon"];
    }
    return _backImageView;
}

- (UIButton *)registerButn {
    if (!_registerButn) {
        UIColor *color = [UIColor orangeColor];
        _registerButn = [UIButton butnWithImageName:nil andTitle:@"注册" andBackgrandImage:@"common_button_white_disable" andFontSize:14 andFontColor:color];
    }
    return _registerButn;
}



@end
